<?php

class ShoutBox extends Delegation {
	
	public function getShoutsLastPage(){
			
		$s = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
		$s->SetMainTable('Discussion', 't');
		$s->AddSelect('CountComments', 't', 'CountComments');		
		$s->AddWhere('t', 'DiscussionID', '', $this->Context->Configuration['SHOUT_DISCUSSIONID'], '=');
		$result = $this->Context->Database->Select($s, $this->Name, 'getShoutsLastPage', 'An error occured getting the last page of the shoutbox.');	
		$row = $this->Context->Database->GetRow($result);
		$postCount = ForceInt($row['CountComments'],0);
		$lastPage = ceil($postCount/$this->Context->Configuration['COMMENTS_PER_PAGE']);
		$lastPost = $postCount-$this->Context->Configuration['COMMENTS_PER_PAGE']*($lastPage-1);
		return $lastPage.'#Item_'.$lastPost;
		
	}
	
	private function renderHeader(){

		return '<h2>'.$this->Context->GetDefinition('ShoutHeader').'</h2>';
	}
	
	private function renderCreator($ShoutPath){
		
		//FIXME: path override
		$ShoutPath = 'extensions/Shoutbox';
		
		$creator = '<div id="ShoutContainer"><textarea id="ShoutText"></textarea><div id="ShoutSubmit">'.$this->Context->GetDefinition('ShoutSubmit').'</div></div>';
		$creator .= '
		<script>
			jQuery(document).ready(function($){
				
				$("#ShoutSubmit").click(function(){
						var shout = $("#ShoutText").val();
						$("#ShoutText").val("");
						if (shout == "") return "";
						
						jQuery.get(
							"'.$ShoutPath.'/ajax.php", {
								shout:  shout
							},
							function(response){
								if(response != ""){
									$("#Shouts").prepend(response);
								}
								else {
									//you fail
								}
							}
						);
				});
			});
		</script>
		';
		return $creator;
	}
	
	private function renderShouts(){

		$s = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
		$s->SetMainTable('Comment', 'c');		
		$s->AddSelect('Body', 'c', 'Body');		
	 	$s->AddSelect(array('DateCreated', 'Body'), 'c');
		$s->AddJoin('User', 'u', 'UserID', 'c', 'AuthUserID', 'left join');
		$s->AddSelect(array('Name', 'Icon'), 'u');
		$s->AddWhere('c', 'DiscussionID', '', $this->Context->Configuration['SHOUT_DISCUSSIONID'], '=');
		$s->AddWhere('c', 'Deleted', '', '0', '=');
		$s->AddWhere('c', 'DateCreated', '', date ("Y-m-d 00:00:00", time() - 86400 * $this->Context->Configuration['SHOUTS_AGE_LIMIT']), '>');
		$s->AddOrderBy('DateCreated', 'c', 'desc');
		$s->AddLimit(0, $this->Context->Configuration['MAX_SHOUTS']);
		
		$result = $this->Context->Database->Select($s, $this->Name, 'renderShouts', 'An error occured getting recent shouts for this forum.');		
		
		$shouts = '';

		while (@$row = $this->Context->Database->GetRow($result)){


			$datediff = floor((time() - strtotime($row['DateCreated']))/time());
			$datediffclass = 'Old';
			$datediffphrase = '-'.$datediff;
			if ($datediff == 0){
				$datediffclass = 'Today';
				$datediffphrase = $this->Context->GetDefinition('ShoutToday');
			}
			else if ($datediff == 1){
				$datediffclass = 'Yesterday';
				$datediffphrase = $this->Context->GetDefinition('ShoutYesterday');
			}

			$pic = ($row['Icon'] != "") ? '<img class="ShoutUserPic" src="'.$row['Icon'].'" /> ' : '';
			$shouts .= '<li><div><span class="ShoutUser '.$datediffclass.'">'.$pic.$row['Name'].'</span>: <span class="ShoutBody">'.$row['Body'].'</span></div></li>';		
		}
		$shouts= '<ul id="Shouts">'.$shouts.'</ul>';
		return $shouts;
	}	
	public function render($ShoutPath){
	
		return $this->renderHeader().$this->renderCreator($ShoutPath).$this->renderShouts();
	}
}	
?>